package com.softtown.modules.dapai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompLeaseOrderDao;
import com.softtown.modules.dapai.dao.CompSaleOrderDao;
import com.softtown.modules.dapai.dto.CompLeaseOrderPageDTO;
import com.softtown.modules.dapai.dto.CompSaleOrderPageDTO;
import com.softtown.modules.dapai.dto.LeaseOrderAddOrUpdateDTO;
import com.softtown.modules.dapai.dto.SaleOrderAddOrUpdateDTO;
import com.softtown.modules.dapai.entity.CompLeaseOrderDetailEntity;
import com.softtown.modules.dapai.entity.CompLeaseOrderEntity;
import com.softtown.modules.dapai.entity.CompSaleOrderDetailEntity;
import com.softtown.modules.dapai.entity.CompSaleOrderEntity;
import com.softtown.modules.dapai.service.CompLeaseOrderDetailService;
import com.softtown.modules.dapai.service.CompLeaseOrderService;
import com.softtown.modules.dapai.service.CompSaleOrderDetailService;
import com.softtown.modules.dapai.service.CompSaleOrderService;
import com.softtown.modules.dapai.vo.CompLeaseOrderVO;
import com.softtown.modules.dapai.vo.CompSaleOrderVO;
import com.softtown.modules.dapai.vo.SaleOrderCustomerVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CompLeaseOrderServiceImpl extends
        BaseServiceImpl<CompLeaseOrderDao, CompLeaseOrderEntity> implements CompLeaseOrderService {


    @Resource
    private CompLeaseOrderDetailService compLeaseOrderDetailService;


    @Override
    public PageInfo<CompLeaseOrderEntity> getLeaseOrderPageList(CompLeaseOrderPageDTO compLeaseOrderPageDTO) {

        Integer pageSize = compLeaseOrderPageDTO.getPageSize();
        Integer page = compLeaseOrderPageDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompLeaseOrderEntity> compSaleOrderEntities =
                baseDao.getLeaseOrderPageList(compLeaseOrderPageDTO.getCompId(), compLeaseOrderPageDTO.getParam());
        return new PageInfo<>(compSaleOrderEntities);
    }


    @Override
    public CompLeaseOrderVO getLeaseOrderById(Long compId, Long id) {
        CompLeaseOrderVO compLeaseOrderVO = new CompLeaseOrderVO();
        compLeaseOrderVO.setCompLeaseOrderEntity(baseDao.getLeaseOrderById(compId, id));
        compLeaseOrderVO.setCompLeaseOrderDetailEntities(baseDao.getOrderDetailById(compId, id));
        return compLeaseOrderVO;
    }

    @Override
    @Transactional
    public void addLeaseOrder(LeaseOrderAddOrUpdateDTO leaseOrderAddOrUpdateDTO) {
        CompLeaseOrderEntity compLeaseOrderEntity =  ConvertUtils
                .sourceToTarget(leaseOrderAddOrUpdateDTO.getCompLeaseOrderDTO(), CompLeaseOrderEntity.class);
        insert(compLeaseOrderEntity);
        List<CompLeaseOrderDetailEntity> compLeaseOrderDetailEntityList = ConvertUtils
                .sourceToTarget(leaseOrderAddOrUpdateDTO.getCompLeaseOrderDetailDTOList(), CompLeaseOrderDetailEntity.class);
        compLeaseOrderDetailEntityList.forEach(item -> {
            item.setCompId(compLeaseOrderEntity.getCompId());
            item.setIsDeleted(IsDeleteEnum.NORMAL.value());
            item.setLeaseOrderId(compLeaseOrderEntity.getId());
        });
        compLeaseOrderDetailService.insertBatch(compLeaseOrderDetailEntityList);
    }

    @Override
    @Transactional
    public void updateLeaseOrder(LeaseOrderAddOrUpdateDTO leaseOrderAddOrUpdateDTO) {
        CompLeaseOrderEntity compLeaseOrderEntity =  ConvertUtils
                .sourceToTarget(leaseOrderAddOrUpdateDTO.getCompLeaseOrderDTO(), CompLeaseOrderEntity.class);
        updateById(compLeaseOrderEntity);
        List<CompLeaseOrderDetailEntity> compLeaseOrderDetailEntityList= compLeaseOrderDetailService.list(new QueryWrapper<CompLeaseOrderDetailEntity>()
                .lambda().eq(CompLeaseOrderDetailEntity::getLeaseOrderId, compLeaseOrderEntity.getId()));
        compLeaseOrderDetailEntityList.forEach(item -> {
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
        });
        compLeaseOrderDetailService.updateBatchById(compLeaseOrderDetailEntityList);

        List<CompLeaseOrderDetailEntity> compLeaseOrderDetailEntityList1 = ConvertUtils
                .sourceToTarget(leaseOrderAddOrUpdateDTO.getCompLeaseOrderDetailDTOList(), CompLeaseOrderDetailEntity.class);
        compLeaseOrderDetailEntityList1.forEach(item -> {
            item.setIsDeleted(IsDeleteEnum.NORMAL.value());
            item.setLeaseOrderId(compLeaseOrderEntity.getId());
        });
        compLeaseOrderDetailService.insertBatch(compLeaseOrderDetailEntityList1);
    }

    @Override
    @Transactional
    public void deleteLeaseOrder(Long compId, Long id) {
        List<CompLeaseOrderDetailEntity> compLeaseOrderDetailEntityList= compLeaseOrderDetailService.list(new QueryWrapper<CompLeaseOrderDetailEntity>()
                .lambda().eq(CompLeaseOrderDetailEntity::getLeaseOrderId,id));
        compLeaseOrderDetailEntityList.forEach(item -> {
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
        });
        compLeaseOrderDetailService.updateBatchById(compLeaseOrderDetailEntityList);


        CompLeaseOrderEntity compLeaseOrderEntity =  baseDao.selectById(id);
        compLeaseOrderEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
        updateById(compLeaseOrderEntity);
    }

}
